/*
 * app_keyScan.c
 *
 *  Created on: 2023年8月30日
 *      Author: zlw
 */

#include "app_keyScan.h"
#include "app_log.h"
#include "softwareMultiTimer.h"
#include "app_pwm.h"
#include "app_led.h"
#include "device.h"
#include "app_api.h"
#include "mac_transmitter.h"
#include "net_packet.h"
#ifdef SM_KP

#define KEYSCAN_CLOCK_PERIOD_MS  2U
#define KEYSCAN_CLOCK_FREQUENCY  (1000U / KEYSCAN_CLOCK_PERIOD_MS)

static void keyscan_driver_hal_start_scan(bool enable);
static void keyInsInit(void);

static void keyscan_driver_hal_init_gpio(void)
{
  uint8_t i;

  for (i = 0; i < KEYSCAN_DRIVER_COLUMN_NUMBER; i++) {
    GPIO->KEYSCANROUTE.ROUTEEN |= (1 << i);
  }

  GPIO->KEYSCANROUTE.COLOUT0ROUTE = (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_0_PORT << _GPIO_KEYSCAN_COLOUT0ROUTE_PORT_SHIFT)
                                    | (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_0_PIN << _GPIO_KEYSCAN_COLOUT0ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_COL_OUT_0_PORT, KEYSCAN_DRIVER_KEYSCAN_COL_OUT_0_PIN, gpioModeWiredAnd, 1);

  GPIO->KEYSCANROUTE.COLOUT1ROUTE = (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_1_PORT << _GPIO_KEYSCAN_COLOUT1ROUTE_PORT_SHIFT)
                                    | (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_1_PIN << _GPIO_KEYSCAN_COLOUT1ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_COL_OUT_1_PORT, KEYSCAN_DRIVER_KEYSCAN_COL_OUT_1_PIN, gpioModeWiredAnd, 1);

  GPIO->KEYSCANROUTE.COLOUT2ROUTE = (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_2_PORT << _GPIO_KEYSCAN_COLOUT2ROUTE_PORT_SHIFT)
                                    | (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_2_PIN << _GPIO_KEYSCAN_COLOUT2ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_COL_OUT_2_PORT, KEYSCAN_DRIVER_KEYSCAN_COL_OUT_2_PIN, gpioModeWiredAnd, 1);

  GPIO->KEYSCANROUTE.COLOUT3ROUTE = (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_3_PORT << _GPIO_KEYSCAN_COLOUT3ROUTE_PORT_SHIFT)
                                    | (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_3_PIN << _GPIO_KEYSCAN_COLOUT3ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_COL_OUT_3_PORT, KEYSCAN_DRIVER_KEYSCAN_COL_OUT_3_PIN, gpioModeWiredAnd, 1);

  GPIO->KEYSCANROUTE.COLOUT4ROUTE = (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_4_PORT << _GPIO_KEYSCAN_COLOUT4ROUTE_PORT_SHIFT)
                                    | (KEYSCAN_DRIVER_KEYSCAN_COL_OUT_4_PIN << _GPIO_KEYSCAN_COLOUT4ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_COL_OUT_4_PORT, KEYSCAN_DRIVER_KEYSCAN_COL_OUT_4_PIN, gpioModeWiredAnd, 1);


  GPIO->KEYSCANROUTE.ROWSENSE0ROUTE = (KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_0_PORT << _GPIO_KEYSCAN_ROWSENSE0ROUTE_PORT_SHIFT)
                                      | (KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_0_PIN << _GPIO_KEYSCAN_ROWSENSE0ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_0_PORT, KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_0_PIN, gpioModeInputPull, 1);

  GPIO->KEYSCANROUTE.ROWSENSE1ROUTE = (KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_1_PORT << _GPIO_KEYSCAN_ROWSENSE1ROUTE_PORT_SHIFT)
                                      | (KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_1_PIN << _GPIO_KEYSCAN_ROWSENSE1ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_1_PORT, KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_1_PIN, gpioModeInputPull, 1);

  GPIO->KEYSCANROUTE.ROWSENSE2ROUTE = (KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PORT << _GPIO_KEYSCAN_ROWSENSE2ROUTE_PORT_SHIFT)
                                      | (KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PIN << _GPIO_KEYSCAN_ROWSENSE2ROUTE_PIN_SHIFT);
  GPIO_PinModeSet(KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PORT, KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PIN, gpioModeInputPull, 1);

  GPIO->KEYSCANROUTE.ROWSENSE3ROUTE = KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PORT << _GPIO_KEYSCAN_ROWSENSE3ROUTE_PORT_SHIFT
                                      | KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PIN << _GPIO_KEYSCAN_ROWSENSE3ROUTE_PIN_SHIFT;
  GPIO->KEYSCANROUTE.ROWSENSE4ROUTE = KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PORT << _GPIO_KEYSCAN_ROWSENSE4ROUTE_PORT_SHIFT
                                      | KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PIN << _GPIO_KEYSCAN_ROWSENSE4ROUTE_PIN_SHIFT;
  GPIO->KEYSCANROUTE.ROWSENSE5ROUTE = KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PORT << _GPIO_KEYSCAN_ROWSENSE5ROUTE_PORT_SHIFT
                                      | KEYSCAN_DRIVER_KEYSCAN_ROW_SENSE_2_PIN << _GPIO_KEYSCAN_ROWSENSE5ROUTE_PIN_SHIFT;
}

static void keyscan_driver_hal_init(void)
{
  sl_keyscan_config_t hw_config;
  uint32_t clock_freq;
  uint32_t clock_div;

  // Set hardware configuration
  hw_config.auto_start_enable = false;
  hw_config.column_number = KEYSCAN_DRIVER_COLUMN_NUMBER;
  hw_config.row_number = KEYSCAN_DRIVER_ROW_NUMBER;
  hw_config.scan_delay = KEYSCAN_DRIVER_SCAN_DELAY_MS;
  hw_config.debounce_delay = KEYSCAN_DRIVER_DEBOUNCE_DELAY_MS;
  hw_config.stable_delay = KEYSCAN_DRIVER_STABLE_DELAY_MS;
  hw_config.single_press_enable = KEYSCAN_DRIVER_SINGLEPRESS;

  // Clock initialization
  CMU_ClockEnable(cmuClock_KEYSCAN, true);

  clock_freq = CMU_ClockFreqGet(cmuClock_KEYSCAN);
  clock_div = clock_freq / KEYSCAN_CLOCK_FREQUENCY - 1U;
  hw_config.clock_divider = clock_div;

  // Hardware initialization
  sl_keyscan_init(&hw_config);

  // Clear interrupt flags
  sl_keyscan_disable_interrupts(_KEYSCAN_IEN_MASK);
  sl_keyscan_clear_interrupts(_KEYSCAN_IEN_MASK);

  // Enable interrupts
  NVIC_ClearPendingIRQ(KEYSCAN_IRQn);
  NVIC_EnableIRQ(KEYSCAN_IRQn);
}

/***************************************************************************//**
 * Enables Keyscan interrupts by passing a set of flags.
 ******************************************************************************/
static void keyscan_driver_hal_enable_interrupts(uint8_t local_flags)
{
  uint32_t keyscan_ien = 0u;

  if (local_flags & KEYSCAN_DRIVER_EVENT_WAKEUP) {
    keyscan_ien |= KEYSCAN_IF_WAKEUP;
  }

  if (local_flags & KEYSCAN_DRIVER_EVENT_KEY) {
    keyscan_ien |= KEYSCAN_IF_KEY;
  }

  if (local_flags & KEYSCAN_DRIVER_EVENT_NOKEY) {
    keyscan_ien |= KEYSCAN_IF_NOKEY;
  }
  sl_keyscan_enable_interrupts(keyscan_ien);
}

/***************************************************************************//**
 * Starts the keyscan scan.
 ******************************************************************************/
static void keyscan_driver_hal_start_scan(bool enable)
{
  if (enable) {
    sl_keyscan_enable();
  }

  sl_keyscan_start_scan();

  sl_keyscan_wait_sync();
}

/***************************************************************************//**
 * Gets the column and row information from the peripheral when a key is pressed.
 ******************************************************************************/
static void keyscan_driver_hal_get_column_row(uint8_t *p_column, uint8_t *p_row)
{
  uint32_t status;
  uint32_t mask = 0UL;

  status = sl_keyscan_get_status();

  /* KEYSCAN_E301: The unused row bits in the KEYSCAN_STATUS field should be masked
     so that unused row bits are set to 1, indicating a key is not pressed. */
  mask = ((1 <<  KEYSCAN_DRIVER_ROW_NUMBER) - 1) << _KEYSCAN_STATUS_ROW_SHIFT;
  status |= (mask ^ _KEYSCAN_STATUS_ROW_MASK);

  *p_column = (status & _KEYSCAN_STATUS_COL_MASK) >> _KEYSCAN_STATUS_COL_SHIFT;
  *p_row = (status & _KEYSCAN_STATUS_ROW_MASK) >> _KEYSCAN_STATUS_ROW_SHIFT;
  /* Invert Row Detection Status (0: pull up for no key press, 1: key press). */
  *p_row = ~*p_row;
  *p_row &= _KEYSCAN_STATUS_ROW_MASK;
}

void keyscan_driver_init(void)
{
  uint8_t local_flags;
  // GPIO setup
  keyscan_driver_hal_init_gpio();
  // Hardware initialization
  keyscan_driver_hal_init();
  // Enable interrupts
  local_flags = KEYSCAN_DRIVER_EVENT_WAKEUP
                | KEYSCAN_DRIVER_EVENT_KEY;
  keyscan_driver_hal_enable_interrupts(local_flags);
  keyscan_driver_hal_start_scan(true);
  keyInsInit();
}

#if 0
/***************************************************************************//**
 * Return if keyscan scan is currently running or not.
 ******************************************************************************/
static bool keyscan_driver_hal_is_scan_running(void)
{
  uint32_t status;

  status = sl_keyscan_get_status();

  return ((status & _KEYSCAN_STATUS_RUNNING_MASK) >> _KEYSCAN_STATUS_RUNNING_SHIFT ? true : false);
}

/***************************************************************************//**
 * Stops the keyscan scan.
 ******************************************************************************/
static void keyscan_driver_hal_stop_scan(void)
{
  sl_keyscan_stop_scan();

  sl_keyscan_wait_sync();

  sl_keyscan_disable();
}
#endif

static bool keyScanSleepState = true;   //键盘有效按键10s后置为true
static bool keyScanWakeUpByMistake = true;   //防止误唤醒0.5s后置为true
static keyPadValue_t keyPadValue = {0x00};

bool key_scan_sleep_state_get(void)
{
  return keyScanSleepState;
}

void key_scan_sleep_state_set(bool state)
{
  keyScanSleepState = state;
}

bool key_pad_wake_up_by_mistake_state_get(void)
{
  return keyScanWakeUpByMistake;
}

void key_value_buffer_clear(void)
{
  memset(&keyPadValue, 0, sizeof(keyPadValue));
}

bool key_value_buffer_get(keyPadValue_t *padValue)
{
  uint8_t i = 0;
  if(keyPadValue.stopBit) {
      DS_app_log_info("recv data : ");
      for(i=0;i<keyPadValue.valueCnt;i++) {
          APP_PRINTF(" 0x%02X ", keyPadValue.value[i]);
      }
      APP_PRINTF("\r\n");
      memcpy(padValue, &keyPadValue, sizeof(keyPadValue_t));
      key_value_buffer_clear();
      return true;
  } else {
      return false;
  }
}

static MultiTimer keyScanTimer[5] = {
    {.Handle = 0},
    {.Handle = 1},
    {.Handle = 2},
    {.Handle = 3},
    {.Handle = 4}
};

static keyIns_t keyDriver[15] = {0};
static void keyTicksHandler(keyIns_t *keyIns, uint8_t keyValue);
extern void SM_alarm_led_start(uint8_t *data);
extern uint8_t s_option_index_value_get(uint8_t index);
static bool alarmSoundState = false;

bool alarmSoundStateGet(void)
{
  return alarmSoundState;
}

static void keyScanWakeupHandler(void)
{
  uint8_t option1 = 0X00;

  option1 = s_option_index_value_get(1);
  keyPad_led_on(BACK_LIGHT);
  if(((option1 >> 4) & 0x01)) {
      keyPad_led_on(FAULT_ORANGE);
  }
  else {
      keyPad_led_off(FAULT_ORANGE);
  }
  if(((option1 >> 5) & 0x01)) {
      keyPad_led_on(DEPLOY_BLUE);
  }
  else {
      keyPad_led_off(DEPLOY_BLUE);
  }
  if(((option1 >> 6) & 0x03) == 0) {
      keyPad_led_off(ALERM_RED);
  } else if(((option1 >> 6) & 0x03) == 1) {
      keyPad_led_on(ALERM_RED);
  } else if(((option1 >> 6) & 0x03) == 2) {
      uint8_t data[3] = {15,0,255};
      SM_alarm_led_start(data);
  }
}

static void keyScanTImerCallback(MultiTimer* timer, void *userData)
{
  (void)userData;
  if(timer->Handle == 0) {
      keyScanSleepState = true;
      set_s_updateMeAppEn(false);
      alarmSoundState = false;
      keyPad_led_off(ALL_GPIO);
      uint8_t data[3] = {0};
      SM_alarm_led_start(data);
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, true, true);
      DS_app_log_error("KST\r\n");
  }
  else if(timer->Handle == 1) {
      for(uint8_t i=0;i<15;i++)
          keyTicksHandler(&keyDriver[i], 0);
  }
  else if(timer->Handle == 2) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, true, false);
  }
  else if(timer->Handle == 3) {
      keyScanWakeUpByMistake = true;
  }
  else if(timer->Handle == 4) {
      keyScanWakeupHandler();
  }
}

//extern void pwm_blink_start(uint8_t duty, uint16_t frequency, uint8_t *pwmData);
extern void keyPad_output_keyValue_report(uint8_t state);

void keyValueCallback(void *data)
{
  keyIns_t * keyIns = (keyIns_t *) data;
  uint8_t option0 = s_option_index_value_get(0);
  uint8_t option2 = s_option_index_value_get(2);

  if(keyIns->event == KEY_DOWN) {
      if(((option0>>2)&0x01) == 0)
        pwm_blink_start(SOUND_BLINK_TYPE_KEY_PRESS, 1, true, false);
      if(keyPadValue.valueCnt < VALUE_COUNT_MAX) {
          keyPadValue.value[keyPadValue.valueCnt++] = keyIns->keyValue;
          if(keyIns->keyValue > 0x1A)
            keyPadValue.stopBit = 1;
          else if(keyIns->keyValue == 0x1A) {
              key_value_buffer_clear();
          }
      }
      else {
          key_value_buffer_clear();
      }
      if(false == device_test_sleep_state_get()) {
          DS_app_log_error("K:0x%02X\r\n", keyIns->keyValue);
      }
  }
  else if(keyIns->event == KEY_LONG) {
      if(keyIns->keyValue == 0x11 && ((option2 & 0x01) == 0)) {
          alarmSoundState = true;
          keyPadValue.value[keyPadValue.valueCnt--] = 0;
          pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, false, false);
          pwm_blink_start(SOUND_BLINK_TYPE_OPTION_FAIL, 1, false, true);
          pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_FIRE, true, false);
          keyPad_output_keyValue_report(keyIns->keyValue);
          softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[2], 0);
          softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[2], 10*32768, keyScanTImerCallback,NULL,0);
      }
      else if(keyIns->keyValue == 0x12 && ((option2 & 0x01) == 0)) {
          keyPadValue.value[keyPadValue.valueCnt--] = 0;
          pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, false, false);
          pwm_blink_start(SOUND_BLINK_TYPE_OPTION_FAIL, 1, false, true);
          if(((option0>>3)&0x01) == 0) {
              alarmSoundState = true;
              pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_SOS, true, false);
          }
          keyPad_output_keyValue_report(keyIns->keyValue);
          softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[2], 0);
          softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[2], 10*32768, keyScanTImerCallback,NULL,0);
      }
      else if(keyIns->keyValue == 0x13 && ((option2 & 0x01) == 0)) {
          keyPadValue.value[keyPadValue.valueCnt--] = 0;
          pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, false, false);
          pwm_blink_start(SOUND_BLINK_TYPE_OPTION_FAIL, 1, false, true);
          if(((option0>>6)&0x01) == 0) {
              alarmSoundState = true;
              pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_MEDICAL, true, false);
          }
          keyPad_output_keyValue_report(keyIns->keyValue);
          softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[2], 0);
          softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[2], 10*32768, keyScanTImerCallback,NULL,0);
      }
  }
}

static void keyInsInit(void)
{
  uint8_t i=0;
  for(i=0;i<15;i++) {
      keyDriver[i].event = NONE_EVENT;
      keyDriver[i].keyValue = 0x10 + i;
      keyDriver[i].ticks = 0;
      keyDriver[i].cb[KEY_DOWN] = keyValueCallback;
      keyDriver[i].cb[KEY_LONG] = keyValueCallback;
  }
}

/***************************************************************************//**
 * Analyzes the keypad data and sets the key(s) press.
 ******************************************************************************/

static void keyTicksHandler(keyIns_t *keyIns, uint8_t keyValue)
{
  switch(keyIns->event) {
    case NONE_EVENT:
      if(keyIns->keyValue == keyValue) {
          keyIns->ticks ++;
          keyIns->event = KEY_DOWN;
          keyIns->cb[KEY_DOWN](keyIns);
      } else {
          keyIns->ticks = 0;
      }
      break;
    case KEY_DOWN:
      if(keyIns->keyValue == keyValue) {
          keyIns->ticks ++;
          if(keyIns->ticks == LONG_PRESS_TIMEOUT) {
              keyIns->event = KEY_LONG;
              keyIns->cb[KEY_LONG](keyIns);
              keyIns->event = KEY_DOWN;
          }
      } else {
          if(keyIns->ticks < SHORT_PRESS_TIMEOUT) {
              keyIns->event = KEY_CLICK;
          }
          keyIns->ticks = 0;
          keyIns->event = NONE_EVENT;
      }
      break;
    default:
      break;
  }
}

static void keyscan_set_keypress(void)
{
  uint8_t column;
  uint8_t row;
  static uint8_t keyPad[5][3] = {
      {0x16, 0x15, 0x14},
      {0x19, 0x18, 0x17},
      {0x1B, 0x10, 0x1A},
      {0x1D, 0x1C, 0x1E},
      {0x13, 0x12, 0x11},
  };

  softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[1], 0);
  softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[1], 4915, keyScanTImerCallback,NULL,0);
  keyscan_driver_hal_get_column_row(&column, &row);
  uint8_t bit = (row>>1 & 0xff);
  for(uint8_t i=0;i<15;i++)
      keyTicksHandler(&keyDriver[i], keyPad[column][bit]);
}
/***************************************************************************//**
 * KEYSCAN interrupt handler.
 ******************************************************************************/

void KEYSCAN_IRQHandler(void)
{
  uint32_t irq_flags;
  uint8_t option0 = s_option_index_value_get(0);
  uint8_t timeout = 10;

  // Retrieve Keyscan interrupt flags
  irq_flags = sl_keyscan_get_interrupts();
  // Interrupt "wake-up from sleep" handling.
  if (irq_flags & KEYSCAN_IF_WAKEUP) {
      DS_app_log_error("pW\r\n");
      keyScanWakeUpByMistake = false;
      keyscan_driver_hal_start_scan(true);
      if(keyScanTimer[3].status == EN_MULTITIMER_STATUS_IDLE)
        softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[3], 16384, keyScanTImerCallback,NULL,0);
  }
  // Interrupt "key pressed detected" handling.
  if(irq_flags & KEYSCAN_IF_KEY) {
      DS_app_log_error("pK\r\n");
      set_s_updateMeAppEn(true);
      if(((option0>>7)&0x01) == 1) {
          timeout = 3;
          if(keyScanSleepState == true) {
              setMacTransEvent(EN_TRANS_REQ_SPVN, 0x00, ADHOC_RETRY_COUNT_MAX ,LBT_REDO_COUNT_MAX);
              pwm_blink_start(SOUND_BLINK_TYPE_KEYPAD_LOCK, 3, false, true);
              softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[0], timeout*32768, keyScanTImerCallback,NULL,0);
          }
      }
      else {
          keyscan_set_keypress();
          if(keyScanSleepState == true) {
              setMacTransEvent(EN_TRANS_REQ_SPVN, 0x00, ADHOC_RETRY_COUNT_MAX ,LBT_REDO_COUNT_MAX);
              softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[4], 3277, keyScanTImerCallback,NULL,0);
          }
          if(((option0>>4)&0x03) == 0)
            timeout = 10;
          else if(((option0>>4)&0x03) == 1)
            timeout = 15;
          else if(((option0>>4)&0x03) == 2)
            timeout = 20;
          else if(((option0>>4)&0x03) == 3)
            timeout = 25;

          softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[0], 0);
          softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &keyScanTimer[0], timeout*32768, keyScanTImerCallback,NULL,0);
      }
      keyScanSleepState = false;
  }

  // Clear all interrupt flags
  sl_keyscan_clear_interrupts(irq_flags);
}
#endif


